<template>
  <div class="dashboard-line-box">
    <div class="dashboard-line-title">收到邮件数量</div>
    <div ref="echart" class="dashboard-line"></div>
  </div>
</template>
<script setup>
import * as echarts from 'echarts'
import {
  nextTick,
  onBeforeMount,
  onMounted,
  onUnmounted,
  ref,
  shallowRef,
} from 'vue'
import { useUserStore } from '@/pinia/modules/user'
import { getEmailINBOXList } from '@/api/emailINBOX'
// import 'echarts/theme/macarons'
var data = ref([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

const userStore = useUserStore()
const uuid = ref(userStore.userInfo.uuid)
const tableData = ref([])
// 查询
const getTableData = async () => {
  const table = await getEmailINBOXList({
    page: 1,
    pageSize: 1000,
    auth: uuid.value,
  })
  if (table.code === 0) {
    tableData.value = table.data.list
  }
  for (let i = 0; i < tableData.value.length; i++) {
    const m = new Date(tableData.value[i].emailDate).getMonth()
    data.value[m]++
  }
  console.log(
    '%c 🎂 data1: ',
    'font-size:20px;background-color: #EA7E5C;color:#fff;',
    data.value
  )
  setOptions()
}

getTableData()

var dataAxis = []
for (var i = 1; i < 13; i++) {
  dataAxis.push(`${i}月`)
}

var yMax = 50
var dataShadow = []

// eslint-disable-next-line no-redeclare
for (var i = 0; i < data.length; i++) {
  dataShadow.push(yMax)
}

const chart = shallowRef(null)
const echart = ref(null)
const initChart = () => {
  chart.value = echarts.init(echart.value /* 'macarons'*/)
  setOptions()
}
const setOptions = () => {
  console.log(
    '%c 🎂 data2: ',
    'font-size:20px;background-color: #EA7E5C;color:#fff;',
    data.value
  )
  chart.value.setOption({
    grid: {
      left: '40',
      right: '20',
      top: '40',
      bottom: '20',
    },
    xAxis: {
      data: dataAxis,
      axisTick: {
        show: false,
      },
      axisLine: {
        show: false,
      },
      z: 10,
    },
    yAxis: {
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        textStyle: {
          color: '#999',
        },
      },
    },
    dataZoom: [
      {
        type: 'inside',
      },
    ],
    series: [
      {
        type: 'bar',
        barWidth: '40%',
        itemStyle: {
          borderRadius: [5, 5, 0, 0],
          color: '#188df0',
        },
        emphasis: {
          itemStyle: {
            color: '#188df0',
          },
        },
        data: data.value,
      },
    ],
  })
}

onMounted(async () => {
  await nextTick()
  initChart()
})

onUnmounted(() => {
  if (!chart.value) {
    return
  }
  chart.value.dispose()
  chart.value = null
})
</script>
<style lang="scss" scoped>
.dashboard-line-box {
  .dashboard-line {
    background-color: #fff;
    height: 360px;
    width: 100%;
  }
  .dashboard-line-title {
    font-weight: 600;
    margin-bottom: 12px;
  }
}
</style>
